import time
import pygame
from pygame.locals import *
from HeroPlane import *
from EnemyPlane import *

def main():
  # 创建窗口
  screen = pygame.display.set_mode((480, 852), 0, 32)
  # 创建一个背景图片
  background = pygame.image.load('src/assets/images/background.png')
  # 创建一个飞机
  hero = HeroPlane(screen)
  # 创建一个敌机
  enemy = EnemyPlane(screen)
  
  while True:
    screen.blit(background, (0, 0))

    hero.display()

    enemy.display()
    enemy.move()
    enemy.fire()
    
    checkKeyboard(hero)
    pygame.display.update()
    time.sleep(0.03)


def checkKeyboard(hero):

  for event in pygame.event.get():
    # 判断是否是点击了退出按钮
    if event.type == QUIT:
      print('exit')
      exit()
    # 判断是否是按下了键
    elif event.type == KEYDOWN:
      # 检测按键是否是a或者left
      if event.key == K_a or event.key == K_LEFT:
        # exit('left')
        hero.moveLeft()
      # 检测按键是否是d或者right
      elif event.key == K_d or event.key == K_RIGHT:
        hero.moveRight()
      # 检测按键是否是w或者up
      elif event.key == K_w or event.key == K_UP:
        hero.moveUp()
      # 检测按键是否是w或者up
      elif event.key == K_s or event.key == K_DOWN:
        hero.moveDown()
      # 检测按键是否是空格键
      if event.key == K_SPACE:
        hero.fire()


if __name__ == '__main__':
  main()
